package com.lhy.wallet.exception;

import com.lhy.wallet.common.ApiRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @author: 卢辉宇
 * @date: 2023/3/10 16:50
 * @description: 拦截处理统一异常
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /*通过LoggerFactory得到log来进行在控制台的打印显示*/
    static final Logger log =  LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //捕获的是系统异常
    @ExceptionHandler(Exception.class)
    public ApiRestResponse handlerException(Exception e){
        log.error("Default Exception",e);
        return ApiRestResponse.error(ExceptionEnum.SYSTEM_ERROR);
    }

    /*在service层的异常向上抛出时，返回给前端的提示封装 成一致的json对象*/
    //捕获的自定义异常
    @ExceptionHandler(MalletException.class)
    public ApiRestResponse handlerMallException(MalletException e){
        log.error("MalletException",e);
        return ApiRestResponse.error(e.getCODE(),e.getMSG());
    }

}
